<template>
  <!-- 扫码商品入库 -->
  <view class="stock-entry">
    <view class="info-card">
      <up-image :show-loading="true" :src="shopData.image" width="48px" height="48px" @click="click"></up-image>
      <view class="product-info">
        <view class="product-name">{{ shopData.shopName }}</view>
        <view class="product-code">款号：{{ shopData.productCode }}</view>
        <view class="product-spec">规格：{{ shopData.spec }}</view>
      </view>
    </view>
    <view class="store-card">
      <view class="store-name">{{ shopName }}</view>
      <view class="quantity-card">
        <view class="label">入库数量</view>
        <view class="number-input">
          <view class="btn minus" :disabled="shopData.inStockQuantity <= 1" @click="decreaseQuantity">-</view>
          <input type="number" v-model="shopData.inStockQuantity" class="input" />
          <view class="btn plus" @click="increaseQuantity">+</view>
        </view>
      </view>
    </view>
    <view class="footer">
      <button class="confirm-btn" @click="handleConfirm" :disabled="loading">
        <up-loading-icon class="loading" color="#fff" size="16" :show="loading"></up-loading-icon>
        <text>确定</text>
      </button>
    </view>
    <!-- 入库成功 -->
    <view class="success-page" v-if="showSuccessPage">
      <view class="success-icon">
        <u-icon name="checkmark-circle" color="rgba(43, 164, 113, 1)" size="80"></u-icon>
      </view>
      <text class="success-text">入库成功</text>
      <view class="button-group">
        <button v-if="from == 'num' || from == 'skunum'" class="action-btn primary-btn" @click="handleAgain">继续手动输入</button>
        <button v-else class="action-btn primary-btn" @click="handleContinue">继续扫码入库</button>
        <button class="action-btn secondary-btn" @click="handleComplete">完成</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { shopApi } from "@/api";
import { getStorage, setStorage } from "@/utils/storage";
const loading = ref(false);
const shopName = ref("");
const quantity = ref(1);
const skuId = ref(1);
const from = ref('')
const shopData = ref({
  shopName: "",
  productCode: "",
  spec:'',
  inStockQuantity: 1
});
let showSuccessPage = ref(false);
const decreaseQuantity = () => {
  if (shopData.value.inStockQuantity > 1) {
    shopData.value.inStockQuantity--;
  }
};

const increaseQuantity = () => {
  shopData.value.inStockQuantity++;
};
onLoad((options) => {
  console.log(options,123)
  skuId.value = options.skuId;
  from.value = options.from
  shopName.value = getStorage("shopName");
  if(options.code){
    getScanCode(options.code)
  }
});

const handleConfirm = () => {
  loading.value = true;
  shopApi
    .SHOPSTOCKINSTOCK({
      inStockProducts: [shopData.value],
      inStockType: 1
    })
    .then((res) => {
      loading.value = false;
      if (res.code != 200) {
        uni.showToast({
          title: res.errMsg,
          icon: "none"
        });
        return;
      }
      showSuccessPage.value = true;
    });
};
const handleContinue = () => {
  showSuccessPage.value = false;
  uni.scanCode({
    success: (res) => {
      // TODO: 不做跳转，直接调用接口 刷新当前页面数据
      shopData.value={
        shopName: "",
        productCode: "",
        spec: "",
        inStockQuantity: 1
      }
      getScanCode(res.result)

    }
  });
};

const getScanCode = (code) => {
  shopApi.GETFRONTPRODUCTINSTOCKBYSKUCODE({
    productSkuCode: code
  }).then((res) => {
    if (res.code != 200) {
      uni.showToast({
        title: res.errMsg,
        icon: "none"
      });
      return;
    }
    showSuccessPage.value = false;
    shopData.value = res.result;
    shopData.value.inStockQuantity =  1;

    shopData.value.image = res.result.image;
    shopData.value.inStockQuantity = res.result.inStockQuantity;
    shopData.value.orderId = res.result.orderId;
    shopData.value.price = res.result.price;

    shopData.value.productCode = res.result.productCode;
    shopData.value.productId = res.result.productId;
    shopData.value.productName = res.result.productName;
    shopData.value.productSkuCode = res.result.productSkuCode;
    shopData.value.productSkuId = res.result.productSkuId;
    console.log(shopData.value,'11111');
  });
}

const handleComplete = () => {
  uni.navigateBack({
    delta: from.value == 'skunum' ? 3 : 2
  });
};

const handleAgain = () => {
  uni.navigateBack({
    delta: from.value == 'skunum' ? 2 : 1
  });
}
</script>
<style lang="scss">
page {
  background-color: #f3f3f3;
}
</style>
<style lang="scss" scoped>
.stock-entry {
  padding: 24rpx 24rpx 120rpx 24rpx;
  box-sizing: border-box;
  position: relative;
  box-sizing: border-box;
  .info-card {
    background-color: #fff;
    border-radius: 12rpx;
    padding: 24rpx;
    box-sizing: border-box;
    margin-bottom: 20rpx;
    display: flex;
  }

  .info-card {
    .product-info {
      margin-left: 20rpx;

      .product-name {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 10rpx;
      }

      .product-code,
      .product-spec {
        font-size: 24rpx;
        color: #666;
        margin-bottom: 6rpx;

        &:last-child {
          margin-bottom: 0;
        }
      }
    }
  }

  .store-card {
    font-size: 28rpx;
    color: #333;
    background-color: #fff;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 20rpx;
    box-sizing: border-box;

    .store-name {
      font-size: 12px;
      font-weight: 400;
      line-height: 20px;
      color: rgba(0, 0, 0, 0.9);
      margin-bottom: 24rpx;
    }
  }

  .quantity-card {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .label {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 20rpx;
      display: block;
    }

    .number-input {
      display: flex;
      align-items: center;
      justify-content: center;

      .btn {
        width: 56rpx;
        height: 56rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #f5f5f5;
        border: none;
        border-radius: 8rpx;

        &:active {
          opacity: 0.8;
        }

        &.disabled {
          opacity: 0.5;
          pointer-events: none;
        }

        text {
          font-size: 36rpx;
          color: #333;
        }
      }

      .input {
        width: 120rpx;
        height: 56rpx;
        text-align: center;
        margin: 0 20rpx;
        background-color: #f5f5f5;
        border: none;
        border-radius: 8rpx;
        font-size: 28rpx;
      }
    }
  }

  .footer {
    position: fixed;
    bottom: 20rpx;
    left: 0;
    right: 0;
    padding: 24rpx;
    background-color: #fff;
    box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
    z-index: 9;

    .confirm-btn {
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      background-color: rgba(214, 142, 68, 1);
      color: #fff;
      border: none;
      border-radius: 40rpx;
      font-size: 32rpx;
      display: flex;
      justify-content: center;
      &:active {
        opacity: 0.9;
      }
    }
  }

  .success-page {
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 120rpx 40rpx;
    box-sizing: border-box;
    z-index: 99;
    width: 100%;
    height: 100vh;
    .success-icon {
      margin-bottom: 40rpx;
    }

    .success-text {
      font-size: 36rpx;
      color: #333;
      font-weight: bold;
      margin-bottom: 100rpx;
    }

    .button-group {
      width: 100%;
      display: flex;
      flex-direction: column;
      gap: 20rpx;
      .action-btn {
        width: 100%;
        height: 88rpx;
        line-height: 88rpx;
        border-radius: 44rpx;
        font-size: 32rpx;
        border: none;

        &.primary-btn {
          background-color: rgba(214, 142, 68, 1);
          color: #fff;
        }

        &.secondary-btn {
          background-color: rgba(253, 236, 219, 1);
          color: rgba(214, 142, 68, 1);
        }
      }
    }
  }
}
</style>
